package com.zhongyu.controller;

import com.zhongyu.pojo.Role;
import com.zhongyu.pojo.User;
import com.zhongyu.service.PermissionService;
import com.zhongyu.service.RoleService;
import com.zhongyu.vo.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.logging.Logger;
/*
角色
 */
@RestController
@RequestMapping("/sys/role")
//@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;


    private String msg;
    //@RequiresPermissions("role:list")
    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, RoleVo roleVo){
        PageResult pageResult=roleService.searchAll(pageNo,pageSize,roleVo);
        return ResultVo.success(pageResult);

    }
   /* @GetMapping("/list")
    public List<Role> list(){
        return roleService.searchAll();

    }*/
   @GetMapping("/list")
   @RequiresPermissions("role:list")
   public ResultVo list(){
       List<Role> list= roleService.searchAll();
       return  ResultVo.success(list);

   }
    @GetMapping("/list1")
    public ResultVo list1(Integer id){
        List<Role> list= roleService.searchById(id);
        return  ResultVo.success(list);

    }

   @PostMapping("/add")
   @RequiresPermissions("role:add")
public ResultVo add(@RequestBody Role role, HttpSession session){
       try {
           role.setCreationDate(LocalDateTime.now());
         User loginUser= (User) session.getAttribute("loginUser");
          if (loginUser!=null){
              role.setCreatedBy(loginUser.getId());
          }
           roleService.add(role);
          msg="添加角色成功!";
         // log.info(msg);
           return ResultVo.success(msg);
       }catch (Exception ex){
          msg="添加角色失败!";
      //    log.error(msg,ex);
           return ResultVo.failure(msg,ex);
       }
     /* roleService.add(role);
      msg="添加角色成功!";
      return ResultVo.success(msg);*/
}
    @PostMapping("/update")
    @RequiresPermissions("role:update")
    public ResultVo update(@RequestBody Role role,HttpSession session) {
       try {
           role.setModifyDate(LocalDateTime.now());
           User loginUser= (User) session.getAttribute("loginUser");
           if (loginUser!=null){
               role.setCreatedBy(loginUser.getId());
           }
           role.setModifyDate(LocalDateTime.now());
           roleService.update(role);
           return ResultVo.success("修改角色成功!");
       }catch (Exception ex){

           return ResultVo.failure("角色修改失败!",ex);
       }
    }
  /* @PostMapping("/update")
   public ResultVo update( Role role,HttpSession session) {
       try {
           //role.setModifyDate(LocalDateTime.now());
           User loginUser= (User) session.getAttribute("loginUser");
           if (loginUser!=null){
               role.setCreatedBy(loginUser.getId());
           }
           roleService.update(role);
           return ResultVo.success("修改角色成功!");
       }catch (Exception ex){

           return ResultVo.failure("角色修改失败!",ex);
       }
   }*/


   /* @PostMapping("/update")
    public ResultVo update(@RequestParam Role role,HttpSession session) {
        try {
            role.setModifyDate(LocalDateTime.now());
            User loginUser= (User) session.getAttribute("loginUser");
            if (loginUser!=null){
                role.setCreatedBy(loginUser.getId());
            }
            roleService.update(role);
            return ResultVo.success("修改角色成功!");
        }catch (Exception ex){

            return ResultVo.failure("角色修改失败!",ex);
        }
    }*/

    @GetMapping("/del")
    @RequiresPermissions("role:del")
    public ResultVo del(Integer id) {
        try {
            roleService.del(id);
            return ResultVo.success("删除角色成功!");
        } catch (Exception ex) {

            return ResultVo.failure("删除角色失败！", ex);
        }


 /*   @ExceptionHandler(RuntimeException.class)
    public ResultVo handleException(RuntimeException ex){
        return ResultVo.failure("角色操作失败！");

    }*/
    }
    @GetMapping("/search4Tree")
   // @RequiresPermissions("user:search4Tree")
    public ResultVo search4Tree(Integer id){
        List<TreeNode> list=permissionService.search4Tree(id);
        return ResultVo.success(list);
    }
  /*@GetMapping("/search4Tree/{id}")
  public ResultVo search4Tree(@PathVariable Integer id){
      List<TreeNode> list=permissionService.search4Tree(id);
      return ResultVo.success(list);
  }*/

  @GetMapping("/grant")
  @RequiresPermissions("role:grant")
  public ResultVo grant(Integer roleId,Integer[] ids){
      try {
          permissionService.grant(roleId,ids);
          return ResultVo.success("角色授权成功！");
      }catch (RuntimeException ex){
       return ResultVo.failure("角色授权失败！",ex);
      }
  }
}
